package com.example.shirotest.study;

import com.example.shirotest.entry.HtmlRegex;
import com.example.shirotest.entry.Result;
import com.example.shirotest.utils.HtmlParse;
import com.example.shirotest.utils.HttpUtil;
import com.example.shirotest.utils.NetUtil;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class TestWeb {

    public static void main(String[] args) {

        String url = "http://www.chinaunicom.com.cn/";
        try {
            Document document = HttpUtil.get(url,"utf-8");
            System.out.println(document);

            Elements select = document.select("#xxgk-two-tztg li");
            System.out.println(select);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public static String getHtmls(String url) throws IOException {
        // 1. 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2. 创建GET请求方法实例
        HttpGet httpGet = new HttpGet(url);
        // 3. 调用HttpClient实例来执行GET请求方法，得到response
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4. 读response，判断是否访问成功 2xx表示 成功。
        int status = response.getStatusLine().getStatusCode();

        String html = "";
        if (status >= 200 && status < 300) {
            // 对得到后的实例可以进行处理，例如读取回复体，读取html
            HttpEntity entity = response.getEntity();
            System.out.println(response);
            System.out.println("=======================");
            html = EntityUtils.toString(entity);
            System.out.println(html);
        } else {
            throw new ClientProtocolException("Unexpected response status: " + status);
        }

        // 6. 释放连接
        response.close();
        httpClient.close();

        return html;
    }


    public static String getHtml(String url) throws Exception {
        URL realUrl = new URL(url);
        HttpURLConnection connection =(HttpURLConnection) realUrl.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuffer html = new StringBuffer();
        while ((line = reader.readLine())!= null){
            html.append(line);
        }
        return html.toString();
    }


    public static void getNetContent(String url,HtmlRegex htmlRegex){
        //String url = "https://www.spzs.com/";   //网站
        String regex = htmlRegex.getCell();  //单元内容

        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }

        Elements elements = doc.select(regex);

        String get_title = "";
        String get_url   = "";
        String get_time  = "";
        String get_cover = "";
        String get_remark= "";
        for (Element element : elements) {
            //抓取标题
            get_title = parseElementByRegex(element,htmlRegex.getTitle_name(),htmlRegex.getTitle_value());

            //抓取地址
            get_url = parseElementByRegex(element,htmlRegex.getUrl_name(),htmlRegex.getUrl_value());

            //抓取时间
            get_time = parseElementByRegex(element, htmlRegex.getTime_name(),htmlRegex.getTime_value());

            //抓取封面图片
            get_cover = parseElementByRegex(element,htmlRegex.getImg_name(),htmlRegex.getImg_value());

            //抓取简介
            get_remark = parseElementByRegex(element,htmlRegex.getRemark_name(),htmlRegex.getRemark_value());


            System.out.println("标题：" + get_title + "\n地址：" + get_url + "\n时间：" + get_time + "\n图片：" + get_cover + "\n简介：" + get_remark);
            System.out.println("==================");

        }
    }

    //解析并获取元素
    public static String parseElementByRegex(Element element,String regex_name,String regex_value){
        if(regex_name==null || regex_name.equals(""))return "";
        if(regex_value==null || regex_value.equals(""))return "";

        String res = "";

        //保留字
        if(regex_value.equalsIgnoreCase("html")){
            if(regex_name.equalsIgnoreCase("this")){
                res = element.text();
            }else{
                res = element.select(regex_name).text();
            }
        } else {
            if(regex_name.equalsIgnoreCase("this")){
                res = element.attr(regex_value);
            }else {
                res = element.select(regex_name).attr(regex_value);
            }
        }

        return res;
    }


    public static void parsejs(){
        //Htmlunit模拟的浏览器，设置css,js等支持及其它的一些简单设置
        WebClient browser = new WebClient();
        browser.getOptions().setCssEnabled(false);
        browser.getOptions().setJavaScriptEnabled(true);
        browser.getOptions().setThrowExceptionOnScriptError(false);

        //获取页面
        HtmlPage htmlPage = null;
        try {
            htmlPage = browser.getPage("https://sf.taobao.com/");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //设置等待js的加载时间
        browser.waitForBackgroundJavaScript(3000);

        //使用xml的方式解析获取到jsoup的document对象
        Document doc = Jsoup.parse(htmlPage.asXml());
        System.out.println(doc);
    }

}
